// pages/Home/travelSearch/travelSearch.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryTravelTitle , queryTravelItem } from '../../../api/travel';
import { throttle } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    CustomBar: wx.getSystemInfoSync().statusBarHeight,
    soltMarginRight: app.globalData.soltMarginRight,
    keyword: '', // 关键词
    page: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
      dataList: [],
    })
    this.getData();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    if(this.data.isReachBottom){
      this._getWithdrawOrderForm()
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 返回
  back(){
    wx.navigateBack();
  },
  // 搜索
  onSearch(e){
    console.log(e.detail)
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
      dataList: [],
      keyword: e.detail,
    })
    this.getData();
  },
  // 获取列表数据
  getData(){
    let _this = this;
    queryTravelTitle({page: _this.data.page,keyword: _this.data.keyword})
    .then(res => {
      if(res.success){
        var list = _this.data.dataList;
        var arr = [];
        for (let index = 0; index < res.data.content.length; index++) {
          const item = res.data.content[index];
          item.imageUrlList = item.titleImgUrl.split(',');
          console.log(item.imageUrlList,'imageUrlList');
          arr.push(item);
        }
        if(_this.data.page == 0){
          list = arr;
        }else{
          list = list.concat(arr);
        }
        _this.setData({
          dataList: list,
          total: res.data.totalElements,
          page: _this.data.page+1,
        })
        if(_this.data.page * 10 >= res.data.totalElements){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 跳转详情
  goDetail(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    console.log(item,'item')
    this._queryTravelItem({itemId: item.itemId,itemType: item.itemType});
  },
  // 获取旅游项目数据
  _queryTravelItem({itemId,itemType}){
    let _this = this;
    queryTravelItem({id: itemId,itemType: itemType})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          var item = res.data.content[0];
          item.title.imageUrlList = item.title.titleImgUrl.split(',');
          for (let j = 0; j < item.itemInfo.length; j++) {
            const itm = item.itemInfo[j];
            itm.imgUrlList = itm.imgUrl.split(',');
          }
          wx.navigateTo({
            url: `/pages/Home/TravelDetail/TravelDetail?item=${JSON.stringify(item)}`,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})